package com.hakim.wfb.platform.controller;

import com.hakim.wfb.customer.params.order.ReceiveAddressParameter;
import com.hakim.wfb.customer.result.order.ReceiveAddressView;
import com.hakim.wfb.customer.service.ReceiveAddressService;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lenovo on 2017/7/15.
 */
@RestController
@RequestMapping(value = "/addr", name = "地址维护")
public class ReceiveAddressController {
    @Autowired
    ReceiveAddressService receiveAddressService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultEx list(ReceiveAddressParameter param, HttpServletRequest request) {

        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();

        param.setRaUser(userid);
        param.setSessionIdentity(sessionIdentity);
        param.setRequestOffset((param.getPageNo() - 1) * param.getPageSize());

        ListResultEx<ReceiveAddressView> res = receiveAddressService.list(param);
        return res;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultEx add(ReceiveAddressParameter param, HttpServletRequest request) {

        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();
        param.setRaUser(userid);
        ResultEx res = receiveAddressService.addAddr(param);
        return res;
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ResultEx delAddr(ReceiveAddressParameter param, HttpServletRequest request) {

        SessionHandler sessionHandler = new SessionHandler();
        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(request, SessionIdentity.class);
        String userid = sessionIdentity.getUuid();
        param.setRaUser(userid);
        ResultEx res = receiveAddressService.delAddr(param);
        return res;
    }

}
